//app.js
App({
  data: {
    mobEndTime: "",
    entryMode: "",
    shareSku: "",
    is_valid_address: "",
    //url: "https://webapidvp.ruemall.com/rongxun-gateway-services/",
    //url: "http://47.95.145.231:8090/rongxun-gateway-services/",
    url: "https://webapi.ruemall.com/rongxun-gateway-services/",
    //url: "http://192.168.1.57:8080/rongxun-gateway-services/",
    //url: "http://192.168.1.222:8082/rongxun-gateway-services/",
    //reUrl: "http://192.168.1.130/base/rest/v3.1.0/"
    //reUrl: "https://dvp.rongxwy.com/base/rest/v3.1.0/"
    reUrl: "https://www.rongxwy.com/base/rest/v3.1.0/"
  },
  wxAjax: function (fun, url, bodyObj, obj) {
    var servername = url.split("/")[url.split("/").length - 1];
    var myDate = new Date();
    var timestamp = myDate.getTime();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    var day = myDate.getDate();
    var hour = myDate.getHours() < 10 ? "0" + myDate.getHours() : myDate.getHours();
    var minute = myDate.getMinutes() < 10 ? "0" + myDate.getMinutes() : myDate.getMinutes();
    var second = myDate.getSeconds() < 10 ? "0" + myDate.getSeconds() : myDate.getSeconds();
    var num = Math.random();
    num = Math.ceil(num * 9999);
    var messageid = year.toString() + month.toString() + day.toString() + hour.toString() + minute.toString() + second.toString() + num;
    var headerObj = {
      "terminal": 4,
      "transaction_type": servername,
      "token": wx.getStorageSync('token'),
      "loginTime": wx.getStorageSync('loginTime'),
      "time_stamp": timestamp,
      "message_id": messageid,
      "mobile": wx.getStorageSync('userid'),
      "version": "2.5.2"
    }
    var headerStr = JSON.stringify(headerObj);
    var bodyStr = JSON.stringify(bodyObj);
    var dataObj = {
      msg: '{"header":' + headerStr + ',"body":' + bodyStr + '}'
    }
    wx.request({
      url: url,
      data: dataObj,
      success: function (res) {
        fun(res, obj)
      },
      fail: function (res) {
        fun(res, obj)
      }
    })
  },
  //登录
  rueLogin: function (fun, obj, srcpage) {
    wx.showLoading({
      title: '加载中..',
    })
    var me = obj;
    me.setData({
      layerflage: 1
    })
    wx.login({
      success(res) {
        var wxcode = res.code;
        wx.getUserInfo({
          success(res) {
            getApp().getReUserInfo(wxcode, res.encryptedData, res.iv, fun, obj)
          },
          fail(res) {
            console.log(res)
            wx.redirectTo({
              url: '../wxauthorized/wxauthorized?from=' + srcpage,
            })
          }
        })
      }
    })
  },
  getReUserInfo: function (wxsmallcode, encrypted, iv, fun, obj) {
    var me = obj;
    var url = getApp().data.reUrl + 'wxopenidService/wxxcxautoregister';
    var dataObj = {
      WXSmallCode: wxsmallcode,
      encryptedData: encodeURIComponent(encrypted),
      iv: encodeURIComponent(iv),
      systemFlag: 'rueMall'
    }
    console.log(dataObj)
    wx.request({
      url: url,
      data: dataObj,
      method: 'POST',
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        wx.setStorageSync('userid', res.data.response.userId);
        wx.setStorageSync('token', res.data.response.token);
        wx.setStorageSync('loginTime', res.data.response.loginTime);
        fun();
      },
      fail: function (res) {
        console.log(res);
        me.setData({
          layerflage: 0
        })
      }
    })
  },
  //获取购物车
  getCartNum(obj) {
    var url = getApp().data.url + 'shoppingcart/getCartCount';
    var body = {
      supermarket_id: wx.getStorageSync('supermarket_id'),
      is_check: '2'
    };
    getApp().wxAjax(this.returnCartNum, url, body, obj);
  },
  returnCartNum(res, obj) {
    obj.setData({
      num: res.data.body.num
    })
  },
  //增加商品
  addPro(obj) {
    console.log(obj)
    var url = getApp().data.url + 'shoppingcart/addShoppingCart';
    var body = {
      quantity: '1',
      supermarket_id: wx.getStorageSync('supermarket_id'),
      sku: obj.data.sku
    };

    if (obj.data.addflage == 1) {
      obj.setData({
        addflage: 2
      })
      getApp().wxAjax(this.addProSug, url, body, obj);
    }
  },
  addProSug(res, obj) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      obj.setData({
        addflage: 1
      })
      wx.showToast({
        title: '添加成功',
      })
      getApp().getCartNum(obj);
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
      obj.setData({
        addflage: 1
      })
    }
  },
  //底部导航
  bottomNav(e) {
    var kind = e.currentTarget.dataset.kind;
    var isPgae = e.currentTarget.dataset.ispage;
    var url;
    if (kind == "index" && isPgae == "1") {
      wx.redirectTo({
        url: '../eIndex/eIndex'
      })
    } else if (kind == "calss" && isPgae == "1") {
      wx.redirectTo({
        url: '../classification/classification'
      })
    } else if (kind == "cart" && isPgae == "1") {
      wx.redirectTo({
        url: '../cart/cart'
      })
    } else if (kind == "my" && isPgae == "1") {
      wx.redirectTo({
        url: '../my/my'
      })
    }
  },
  //倒计时公用方法
  getActiveEndTime: function (obj, nowTimeStr, endTimeStr, fun) {
    endTimeStr = endTimeStr.replace(/-/g, '/');
    nowTimeStr = nowTimeStr.replace(/-/g, '/');
    var nowTime = (new Date(nowTimeStr)).getTime();
    var me = obj;
    clearInterval(interval);
    var interval = setInterval(function () {
      var str = '';
      //取当前时间
      nowTime += 1000;
      //console.log(nowTime)
      var endTime = (new Date(endTimeStr)).getTime();
      endTime = parseInt(endTime / 1000);
      var countdownTime = endTime - parseInt(nowTime / 1000);
      var day;
      var hour;
      var min;
      var sec;
      var allhour;
      day = parseInt(countdownTime / (3600 * 24));
      hour = parseInt((countdownTime - day * (3600 * 24)) / 3600);
      allhour = parseInt(countdownTime / 3600);
      min = parseInt((countdownTime - day * (3600 * 24) - hour * 3600) / 60);
      sec = countdownTime - day * (3600 * 24) - hour * 3600 - min * 60;
      if (sec <= 0 && min <= 0 && hour <= 0) {
        if (fun != undefined) {
          fun();
        }
        sec = "00";
        min = "00";
        hour = "00";
        allhour = "00";
        clearInterval(interval);
      } else {
        if (Number(sec) < 10) {
          sec = "0" + sec;
        }
        if (Number(min) < 10) {
          min = "0" + min;
        }
        if (Number(hour) < 10) {
          hour = "0" + hour;
        }
        if (Number(allhour) < 10) {
          allhour = "0" + allhour;
        }
      }
      var a = '' + allhour + ':' + min + ':' + sec;
      me.setData({
        cuntdown: a,
        hour: allhour,
        min: min,
        sec: sec
      })
    }, 1000);
  },
  //首页倒计时
  getIndexActiveEndTime: function (obj, endTimeStr, fun) {
    var me = obj;
    var str = '';
    var endTime = endTimeStr;
    var date = new Date();
    var nowTime = date.getTime();
    endTime = parseInt(endTime / 1000);
    var countdownTime = endTime - parseInt(nowTime / 1000);
    var day;
    var hour;
    var min;
    var sec;
    var allhour;
    day = parseInt(countdownTime / (3600 * 24));
    hour = parseInt((countdownTime - day * (3600 * 24)) / 3600);
    allhour = parseInt(countdownTime / 3600);
    min = parseInt((countdownTime - day * (3600 * 24) - hour * 3600) / 60);
    sec = countdownTime - day * (3600 * 24) - hour * 3600 - min * 60;
    clearInterval(interval);
    var interval = setInterval(function () {
      var str = '';
      var endTime = (new Date(endTimeStr)).getTime();
      var date = new Date();
      var nowTime = date.getTime();
      endTime = parseInt(endTime / 1000);

      var countdownTime = endTime - parseInt(nowTime / 1000);
      var day;
      var hour;
      var min;
      var sec;
      var allhour;
      day = parseInt(countdownTime / (3600 * 24));
      hour = parseInt((countdownTime - day * (3600 * 24)) / 3600);
      allhour = parseInt(countdownTime / 3600);
      min = parseInt((countdownTime - day * (3600 * 24) - hour * 3600) / 60);
      sec = countdownTime - day * (3600 * 24) - hour * 3600 - min * 60;
      if (sec <= 0 && min <= 0 && hour <= 0) {
        if (fun != undefined) {
          wx.removeStorageSync('indexObj');
          fun();

        }
        sec = "00";
        min = "00";
        hour = "00";
        allhour = "00";
        clearInterval(interval);
      } else {
        if (Number(sec) < 10) {
          sec = "0" + sec;
        }
        if (Number(min) < 10) {
          min = "0" + min;
        }
        if (Number(hour) < 10) {
          hour = "0" + hour;
        }
        if (Number(allhour) < 10) {
          allhour = "0" + allhour;
        }
      }
      var a = '' + allhour + ':' + min + ':' + sec;
      me.setData({
        cuntdown: a,
        hour: allhour,
        min: min,
        sec: sec
      })
    }, 1000);
  },
})